/*
 * @Author: heinan
 * @Date: 2020-07-16 11:33:19
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-09 20:51:07
 */
"use strict";

const { Service } = require("egg");
const { idCreator } = require("../utils");

class FeedBackService extends Service {
  async index({ status, currentPage, pageSize }) {
    const where = {};
    if (status) {
      where.status = status;
    }
    const result = await this.app.mysql.select("feedback", {
      where,
      offset: (currentPage - 1) * pageSize,
      limit: parseInt(pageSize),
    });
    const total = await this.app.mysql.count("feedback", where);
    return {
      data: result,
      total,
      pageSize: Number(pageSize),
      currentPage: Number(currentPage),
    };
  }
  async destroy({ id }) {
    return await this.app.mysql.delete("feedback", { id });
  }
  async update({ id, status, feedback, mobile }) {
    const $data = { id };
    if (status) {
      $data.id = id;
    }
    if (feedback) {
      $data.feedback = feedback;
    }
    if (mobile) {
      $data.mobile = mobile;
    }
    return await this.app.mysql.update("feedback", $data);
  }
  async create({ uid, mobile, feedback }) {
    const $data = {
      uid,
      mobile,
      feedback,
      id: idCreator(),
      create_time: new Date().getTime(),
    };
    return await this.app.mysql.insert("feedback", $data);
  }
}

module.exports = FeedBackService;

